"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 发布订阅
Case Name   : 订阅端使用gs_probackup远程备份
Create At   : 2022-11-07
Owner       : opentestcase004
Description :
    1.在两个集群创建表
    2.创建发布订阅
    3.订阅端创建物理复制槽
    4.订阅端备节点远程全量备份主节点
    5.发布端插入数据
    6.订阅端查询数据是否更新
    7.修改订阅端
    8.订阅端增量备份主节点
    9.使用全量备份恢复远程恢复订阅端主节点
    10.发布端插入表数据
    11.订阅端查询数据是否更新
    12.使用增量备份恢复远程恢复订阅端主节点
    13.发布端修改表数据
    14.订阅端查询数据是否更新
    15.删除订阅端
    16.订阅端增量备份主节点
    17.重建订阅
    18.使用step17中增量备份恢复远程恢复订阅端主节点
    19.发布端删除表数据
    20.订阅端查询数据是否更新
    21.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.数据更新
    7.成功
    8.成功
    9.成功
    10.成功
    11.数据更新
    12.成功
    13.成功
    14.数据更新
    15.成功
    16.成功
    17.成功
    18.成功
    19.成功
    20.数据不更新,订阅信息不存在
    21.成功
History     :
    Modified by opentestcase033 at 2023-01-16:setUp中备份配置文件在发布主机执行两次，
    改为发布、订阅各执行1次，清理环境步骤中同步断言信息
    Modified by opentestcase033 at 2023/2/3:优化修改目录权限步骤，清理环境步骤修改断言
    信息，添加清理不活跃复制槽
    Modified by opentestcase013 at 2023/2/21: 优化用例，增加等待时间及重启操作后再查询
    Modified by opentestcase033 at 2023/11/15: step12、step18中添加延时等待回放完成，集群状态更新
"""

import os
import time
import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.pri_userdb_sub_sta = Node(node='remote1_Standby1DbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub_sta = CommonSH('remote1_Standby1DbUser')
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.com_pub = Common()
        self.com_sub = Common('remote1_PrimaryDbUser')
        self.comsh_sta_sub = []
        self.nodelist = ['remote1_Standby1DbUser', 'remote1_Standby2DbUser']
        for i in range(2):
            self.comsh_sta_sub.append(CommonSH(self.nodelist[i]))
        self.tb_name1 = 't_pub_sub_0067_1'
        self.tb_name2 = 't_pub_sub_0067_2'
        self.subname = "s_pub_sub_0067"
        self.pubname = "p_pub_sub_0067"
        self.slot = "s_pub_sub_slot_0067"
        self.res_dropslots = []
        self.parent_path_pub = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.parent_path_sub = os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1)
        self.backup_path = os.path.join(self.parent_path_sub,
                                        'back_pub_sub_case067')
        self.remote_path = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1), 'app/bin')
        self.remote_libpath = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1), 'app/lib')
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.wal_level = self.com_pub.show_param("wal_level")
        self.enable_cbm_tracking = self.com_sub.show_param(
            "enable_cbm_tracking", macro.DB_ENV_PATH_REMOTE1)
        self.user_param_pub = f'-U {self.pri_userdb_sub.db_user} ' \
            f'-W {self.pri_userdb_sub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
            f'-W {self.pri_userdb_sub.db_password}'
        cmd = f"cp " \
            f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')} " \
            f"{os.path.join(self.parent_path_pub, 'pg_hba.conf')};"
        self.log.info(cmd)
        result = self.pri_userdb_pub.sh(cmd).result()
        self.log.info(result)
        cmd = f"cp " \
            f"{os.path.join(macro.DB_INSTANCE_PATH_REMOTE1, 'pg_hba.conf')}" \
            f" {os.path.join(self.parent_path_sub, 'pg_hba.conf')};"
        self.log.info(cmd)
        result = self.pri_userdb_sub.sh(cmd).result()
        self.log.info(result)
        self.hostname = self.pri_userdb_sub.sh('hostname').result().strip()
        text = "--step:预置条件,修改pg_hba expect:成功"
        self.log.info(text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_sub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    all  {self.pri_userdb_sub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        result = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
        self.assertTrue(result, '执行失败' + text)
        result = self.commsh_pub.restart_db_cluster(True)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败' + text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload ', self.constant.GSGUC_SUCCESS_MSG,
            'enable_cbm_tracking=on', dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host all  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)

    def test_pub_sub(self):
        text = "--step1:在两个集群创建表 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name1};" \
            f"drop table if exists {self.tb_name2};" \
            f"create table {self.tb_name1}(id1 int primary key,id2 int,id3 " \
            f"int);" \
            f"create table {self.tb_name2}(id int primary key constraint " \
            f"id_nn not null,use_filename varchar2(20),filename " \
            f"varchar2(255),text varchar2(2000))" \
            f"partition by range(id)" \
            f"( " \
            f"      partition p1 values less than(30)," \
            f"      partition P2 values less than(60)," \
            f"      partition P3 values less than(90)," \
            f"      partition P4 values less than(MAXVALUE)" \
            f");"
        pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.DROP_TABLE_SUCCESS),
                         2, '执行失败' + text)
        self.assertEqual(pub_result.count(self.constant.TABLE_CREATE_SUCCESS),
                         4, '执行失败' + text)
        sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertEqual(sub_result.count(self.constant.DROP_TABLE_SUCCESS),
                         2, '执行失败' + text)
        self.assertEqual(sub_result.count(self.constant.TABLE_CREATE_SUCCESS),
                         4, '执行失败' + text)

        text = "--step2:创建发布订阅 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop publication if exists {self.pubname};" \
            f"create publication {self.pubname} for table " \
            f"{self.tb_name1},{self.tb_name2} ;"
        pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertIn(self.constant.drop_pub_succ_msg, pub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.create_pub_succ_msg, pub_result,
                      '执行失败' + text)
        result = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(self.constant.create_keycipher_success, result,
                      '执行失败' + text)
        sql_cmd = f"drop subscription if exists {self.subname};" \
            f"create subscription {self.subname} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.ssh_password}' publication " \
            f"{self.pubname};"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.drop_sub_succ_msg, sub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.create_sub_succ_msg, sub_result, '执行失败'
                      + text)

        text = "--step3:订阅端创建物理复制槽 expect:成功--"
        self.log.info(text)
        sub_result = self.commsh_sub.execut_db_sql(
            f"select pg_create_physical_replication_slot('{self.slot}', "
            f"'True')",
            self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(f'({self.slot},)', sub_result, '执行失败' + text)

        text = "--step4:订阅端备节点远程全量备份主节点 expect:成功--"
        self.log.info(text)
        check_cmd = f'if [ -d {self.backup_path} ]; ' \
            f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(check_cmd)
        result = self.pri_userdb_sub_sta.sh(check_cmd).result()
        self.log.info(result)
        if result == 'exists':
            sql_cmd = f"rm -rf {self.backup_path};"
            result = self.pri_userdb_sub_sta.sh(sql_cmd).result()
            self.log.info(result)
        sql_cmd = f"mkdir {self.backup_path};chmod 700 {self.backup_path}"
        self.log.info(sql_cmd)
        result = self.pri_userdb_sub_sta.sh(sql_cmd).result()
        self.log.info(result)
        result = self.commsh_sub_sta.exec_pro_backup_init(self.backup_path,
                                            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertTrue(result, '执行失败' + text)
        result = self.commsh_sub_sta.exec_pro_back_add(
            self.backup_path, 'instance2', 
            other_cmd=f"--remote-proto=ssh "
            f"--remote-host={self.pri_userdb_sub.db_host} --remote-port=22 "
            f"--remote-user={self.pri_userdb_sub.ssh_user} "
            f"--remote-path={self.remote_path} "
            f"--remote-libpath={self.remote_libpath} -j 16",
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertTrue(result, '执行失败' + text)
        result = self.commsh_sub_sta.exec_pro_backup_backup(
            self.backup_path, 'instance2', 'full', self.pri_userdb_sub.db_name,
            other_cmd=f"-U {self.pri_userdb_sub.db_user} -W "
            f"{self.pri_userdb_sub.ssh_password} --remote-proto=ssh "
            f"--remote-host={self.pri_userdb_sub.db_host} "
            f"--remote-port=22 --remote-user={self.pri_userdb_sub.ssh_user} "
            f"--remote-path={self.remote_path} "
            f"--remote-libpath={self.remote_libpath} --backup-pg-replslot -j "
            f"16",
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertTrue(result, '执行失败' + text)

        text = "--step5:发布端插入数据 expect:成功--"
        self.log.info(text)
        pub_result = self.commsh_pub.execut_db_sql(
            f"insert into {self.tb_name1} values(1,1,1);"
            f"insert into {self.tb_name2} values(1,'first','%一','');")
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         '执行失败' + text)

        text = "--step6:订阅端查询数据是否更新 expect:数据更新--"
        self.log.info(text)
        time.sleep(5.5)
        sql_select = f"select * from {self.tb_name1};" \
                     f"select * from {self.tb_name2};" \
                     f"select * from pg_replication_slots;"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn('1 |   1 |   1', sub_result, '执行失败' + text)
        self.assertIn('1 | first        | %一', sub_result, '执行失败' + text)
        self.assertIn(self.slot, sub_result, '执行失败' + text)

        text = "--step7:修改订阅端 expect:成功--"
        self.log.info(text)
        sub_result = self.commsh_sub.execut_db_sql(
            f"alter subscription {self.subname} set (binary=True);", 
            self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.alter_sub_succ_msg, sub_result, '执行失败'
                      + text)

        text = "--step8:订阅端增量备份主节点 expect:成功--"
        self.log.info(text)
        result = self.commsh_sub_sta.exec_pro_backup_backup(
            self.backup_path, 'instance2', 'PTRACK --stream',
            self.pri_userdb_sub.db_name, other_cmd=f" -U "
            f"{self.pri_userdb_sub.db_user} -W "
            f"{self.pri_userdb_sub.ssh_password} --remote-proto=ssh "
            f"--remote-host={self.pri_userdb_sub.db_host} "
            f"--remote-port=22 --remote-user={self.pri_userdb_sub.ssh_user} "
            f"--remote-path={self.remote_path} "
            f"--remote-libpath={self.remote_libpath} --backup-pg-replslot", 
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败' + text)

        text = "--step9:使用全量备份远程恢复订阅端主节点 expect:成功--"
        self.log.info(text)
        restore_mod = ''
        sub_result = self.commsh_sub.stop_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertTrue(sub_result, '执行失败' + text)
        sub_result = self.commsh_sub_sta.exec_probackup_show(
            self.backup_path, 'instance2', env_path=macro.DB_ENV_PATH_REMOTE1)
        result_split = sub_result.splitlines()
        for i in result_split:
            if 'FULL' in str(i):
                restore_mod = str(str(i).split()[2]).strip()
        sub_result = self.commsh_sub_sta.exec_pro_backup_restore(
            self.backup_path, 'instance2', restore_mod,
            '--incremental-mode=checksum --remote-proto=ssh '
            f'--remote-host={self.pri_userdb_sub.db_host} '
            f'--remote-port=22 --remote-user={self.pri_userdb_sub.ssh_user} '
            f'--remote-path={self.remote_path} '
            f'--remote-libpath={self.remote_libpath}', 
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertTrue(sub_result, '执行失败' + text)
        sub_result = self.commsh_sub.start_db_cluster(
            True, env_path=macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in sub_result or 'Degrade' in \
              sub_result
        self.assertTrue(flg, '执行失败' + text)
        time.sleep(5)
        for i in range(2):
            result = self.comsh_sta_sub[i].build_standby(
                "-b full -t 3600", db_path=macro.DB_INSTANCE_PATH_REMOTE1,
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.assertIn(self.constant.BUILD_SUCCESS_MSG, result, '执行失败' +
                          text)
        time.sleep(60)
        sub_result = self.commsh_sub.start_db_cluster(
            True, env_path=macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in sub_result or 'Degrade' in \
              sub_result
        self.assertTrue(flg, '执行失败' + text)
        sub_status = self.commsh_sub.get_db_cluster_status(
            'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_status)
        self.assertEqual(sub_status.count('Normal'), 4, '执行失败' + text)

        text = "--step10:发布端插入表数据 expect:成功--"
        self.log.info(text)
        pub_result = self.commsh_pub.execut_db_sql(
            f"insert into {self.tb_name1} values(2,1,1);"
            f"insert into {self.tb_name2} values(2,'first','%一','');")
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         '执行失败' + text)

        text = "--step11:订阅端查询数据是否更新 expect:数据更新--"
        self.log.info(text)
        time.sleep(5.5)
        sql_select = f"select * from {self.tb_name1};" \
                     f"select * from {self.tb_name2};"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn('2 |   1 |   1', sub_result, '执行失败' + text)
        self.assertIn('2 | first        | %一', sub_result, '执行失败' + text)

        text = "--step12:使用增量备份恢复远程恢复订阅端主节点 expect:成功--"
        self.log.info(text)
        restore_mod = ''
        sub_result = self.commsh_sub.stop_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertTrue(sub_result, '执行失败' + text)
        sub_result = self.commsh_sub_sta.exec_probackup_show(
            self.backup_path, 'instance2', env_path=macro.DB_ENV_PATH_REMOTE1)
        result_split = sub_result.splitlines()
        for i in result_split:
            if 'PTRACK' in str(i):
                restore_mod = str(str(i).split()[2]).strip()
        sub_result = self.commsh_sub_sta.exec_pro_backup_restore(
            self.backup_path, 'instance2', restore_mod,
            '--incremental-mode=checksum --remote-proto=ssh '
            f'--remote-host={self.pri_userdb_sub.db_host} '
            f'--remote-port=22 --remote-user={self.pri_userdb_sub.ssh_user} '
            f'--remote-path={self.remote_path} '
            f'--remote-libpath={self.remote_libpath}', 
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertTrue(sub_result, '执行失败' + text)
        sub_result = self.commsh_sub.start_db_cluster(
            True, env_path=macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in sub_result or 'Degrade' in \
              sub_result
        self.assertTrue(flg, '执行失败' + text)
        time.sleep(5)
        for i in range(2):
            result = self.comsh_sta_sub[i].build_standby(
                "-b full -t 3600", db_path=macro.DB_INSTANCE_PATH_REMOTE1,
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.assertIn(self.constant.BUILD_SUCCESS_MSG, result, '执行失败' +
                          text)
        time.sleep(2)
        sub_status = self.commsh_sub.get_db_cluster_status(
            'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_status)
        self.assertEqual(sub_status.count('Normal'), 4, '执行失败' + text)

        text = "--step13:发布端修改表数据 expect:成功--"
        self.log.info(text)
        pub_result = self.commsh_pub.execut_db_sql(
            f"update {self.tb_name1} set id3=999;"
            f"update {self.tb_name2} set filename='update';")
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.UPDATE_SUCCESS_MSG), 2,
                         '执行失败' + text)

        text = "--step14:订阅端查询数据是否更新 expect:数据更新--"
        self.log.info(text)
        time.sleep(5.5)
        sql_select = f"select id3 from {self.tb_name1};" \
                     f"select filename from {self.tb_name2};"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertEqual(sub_result.count('999'), 1, '执行失败' + text)
        self.assertEqual(sub_result.count('update'), 1, '执行失败' + text)

        text = "--step15:删除订阅端 expect:成功--"
        self.log.info(text)
        sub_result = self.commsh_sub.execut_db_sql(
            f"drop subscription {self.subname};", self.user_param_sub, None,
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.drop_sub_succ_msg, sub_result, '执行失败' +
                      text)

        text = "--step16:订阅端备节点远程增量备份主节点 expect:成功--"
        self.log.info(text)
        result = self.commsh_sub_sta.exec_pro_backup_backup(
            self.backup_path, 'instance2', 'PTRACK --stream',
            self.pri_userdb_sub.db_name, other_cmd=f" -U "
            f"{self.pri_userdb_sub.db_user} -W "
            f"{self.pri_userdb_sub.ssh_password} --remote-proto=ssh "
            f"--remote-host={self.pri_userdb_sub.db_host} "
            f"--remote-port=22 --remote-user={self.pri_userdb_sub.ssh_user} "
            f"--remote-path={self.remote_path} "
            f"--remote-libpath={self.remote_libpath} --backup-pg-replslot", 
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败' + text)

        text = "--step17:重建订阅 expect:成功--"
        self.log.info(text)
        sql_cmd = f"create subscription {self.subname} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.ssh_password}' publication " \
            f"{self.pubname};"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.create_sub_succ_msg, sub_result, '执行失败'
                      + text)

        text = "--step18:使用step16中增量备份恢复远程恢复订阅端主节点 " \
               "expect:成功--"
        self.log.info(text)
        sub_result = self.commsh_sub.stop_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertTrue(sub_result, '执行失败' + text)
        sub_result = self.commsh_sub_sta.exec_probackup_show(
            self.backup_path, 'instance2', env_path=macro.DB_ENV_PATH_REMOTE1)
        result_split = sub_result.splitlines()[3]
        restore_mod = str(str(result_split).split()[2]).strip()
        sub_result = self.commsh_sub_sta.exec_pro_backup_restore(
            self.backup_path, 'instance2', restore_mod,
            '--incremental-mode=checksum --remote-proto=ssh '
            f'--remote-host={self.pri_userdb_sub.db_host} '
            f'--remote-port=22 --remote-user={self.pri_userdb_sub.ssh_user} '
            f'--remote-path={self.remote_path} '
            f'--remote-libpath={self.remote_libpath}', 
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertTrue(sub_result, '执行失败' + text)
        sub_result = self.commsh_sub.start_db_cluster(
            True, env_path=macro.DB_ENV_PATH_REMOTE1)
        flg = self.constant.START_SUCCESS_MSG in sub_result or 'Degrade' in \
              sub_result
        self.assertTrue(flg, '执行失败' + text)
        time.sleep(5)
        for i in range(2):
            result = self.comsh_sta_sub[i].build_standby(
                " -b full -t 3600", db_path=macro.DB_INSTANCE_PATH_REMOTE1,
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.assertIn(self.constant.BUILD_SUCCESS_MSG, result, '执行失败' +
                          text)
        time.sleep(2)
        sub_status = self.commsh_sub.get_db_cluster_status(
            'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_status)
        self.assertEqual(sub_status.count('Normal'), 4, '执行失败' + text)

        text = "--step19:发布端删除表数据 expect:成功--"
        self.log.info(text)
        pub_result = self.commsh_pub.execut_db_sql(
            f"delete from {self.tb_name1};")
        self.log.info(pub_result)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, pub_result, '执行失败'
                      + text)

        text = "--step20:订阅端查询数据是否更新 expect:数据不更新,订阅信" \
               "息不存在--"
        self.log.info(text)
        time.sleep(5.5)
        sql_select = f"select * from {self.tb_name1};" \
                     f"select * from pg_subscription;"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn('1 |   1 | 999', sub_result, '执行失败' + text)
        self.assertNotIn(self.subname, sub_result, '执行失败' + text)

    def tearDown(self):
        text = "--step21:清理环境 expect:成功--"
        self.log.info(text)
        sql_cmd = f"rm -rf {self.backup_path};ls -al {self.backup_path};"
        self.log.info(sql_cmd)
        sub_result1 = self.pri_userdb_sub_sta.sh(sql_cmd).result()
        self.log.info(sub_result1)
        sql_cmd = f"drop subscription if exists {self.subname};"
        drop_sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_result)
        sql_cmd = f"drop publication if exists {self.pubname};"
        drop_pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(drop_pub_result)
        undel_slots = f"select slot_name from pg_replication_slots " \
                      f"where slot_type = 'logical' " \
                      f"and plugin = 'pgoutput' and active = false;"
        res_undel = self.commsh_pub.execut_db_sql(undel_slots)
        self.log.info(res_undel)
        for undel_slot in res_undel.splitlines()[2:-1]:
            res_dropslot = self.commsh_pub.execut_db_sql(
                f"select pg_drop_replication_slot('{undel_slot.strip()}')")
            self.log.info(res_dropslot)
            self.res_dropslots.append(res_dropslot)
        sql_cmd = f"drop table if exists {self.tb_name1};" \
                  f"drop table if exists {self.tb_name2};"
        result_sub = self.commsh_sub.execut_db_sql(sql_cmd, self.user_param_sub,
                                                None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result_sub)
        result_pub = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(result_pub)
        cmd = f"mv " \
            f"{os.path.join(self.parent_path_pub, 'pg_hba.conf')} "\
            f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')} "
        self.log.info(cmd)
        sh_result = self.pri_userdb_pub.sh(cmd).result()
        self.log.info(sh_result)
        cmd = f"mv " \
            f"{os.path.join(self.parent_path_sub, 'pg_hba.conf')} "\
            f"{os.path.join(macro.DB_INSTANCE_PATH_REMOTE1, 'pg_hba.conf')} "
        self.log.info(cmd)
        sh_result1 = self.pri_userdb_sub.sh(cmd).result()
        self.log.info(sh_result1)
        result1 = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        self.log.info(result1)
        result2 = self.commsh_sub.execute_gsguc(
            'reload ', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_cbm_tracking={self.enable_cbm_tracking}',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result2)
        pub_result = self.commsh_pub.restart_db_cluster(True)
        sub_result = self.commsh_sub.restart_db_cluster(
            True, macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(f"No such file or directory", sub_result1,
                      '执行失败' + text)
        self.assertTrue(pub_result, '执行失败' + text)
        self.assertTrue(sub_result, '执行失败' + text)
        self.assertTrue(result1, '执行失败' + text)
        self.assertTrue(result2, '执行失败' + text)
        self.assertEqual("", sh_result, '执行失败' + text)
        self.assertEqual("", sh_result1, '执行失败' + text)
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_result,
                      '执行失败' + text)
        for i in self.res_dropslots:
            self.assertIn('(1 row)', i, f'删除残留复制槽{i}失败:{text}')
        self.assertEqual(result_sub.count(self.constant.DROP_TABLE_SUCCESS), 2,
                         '执行失败' + text)
        self.assertEqual(result_pub.count(self.constant.DROP_TABLE_SUCCESS), 2,
                         '执行失败' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
